package www.bplead.com.mydemo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import www.bplead.com.mydemo.R;

/**
 * Created by wangyangzi on 2016/9/30.
 */
public class ScrollDeleteListView extends ListView implements View.OnTouchListener {
    private static final String TAG = "ScrollDeleteListView";
    //列表内容
    View containerView;
    //删除按钮
    View delView;
    //删除按钮的长度
    static int delBtn_width;

    public ScrollDeleteListView(Context context) {
        super(context);
        initView(context);
    }

    public ScrollDeleteListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ScrollDeleteListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View scrollView = inflater.inflate(R.layout.scroll_delete_item, null);
        containerView = scrollView.findViewById(R.id.list_item_content);
        delView = scrollView.findViewById(R.id.btn_scroll_delete);

        //测量delView在父布局中所占宽高
        delView.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        delBtn_width = delView.getMeasuredWidth();

        this.setOnTouchListener(this);
    }

    int oldX = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int currentX = (int) event.getX();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                containerView.setPadding(80,0,0,0);
                containerView.invalidate();
                oldX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                move(oldX,currentX);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return false;
    }

    //listView内容左边距
    int marginLeft = 0;
    //删除按钮右边距
    int marginRight = -80;
    private void move(int oldX,int currentX) {
        int dis = currentX - oldX;
        //向左滑动
        if(dis < 0){
            setMargins(containerView,dis,0,0,0);
            setMargins(delView,0,delBtn_width - dis,0,0);

            Log.v(TAG,"move to left>>" + dis);
        }
        //向右移动
        else if(dis > 0){
            Log.v(TAG,"move to right>>" + dis);
        }
    }

    public static void setMargins(View view,int left,int top,int right,int bottom){
        if(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams){
            ViewGroup.MarginLayoutParams p = (MarginLayoutParams) view.getLayoutParams();
            p.setMargins(left,top,right,bottom);
            view.requestLayout();
            Log.v(TAG,"-----------------------");
        }
    }
}
